package com.xtaller.lazy.lib.convert;

import com.alibaba.fastjson.JSONObject;
import lombok.var;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-01
 * @Description: fastJson处理类
 */
public class J extends JSONObject {

    public static String o2s(Object obj){
        return J.toJSONString(obj);
    }

    public static JSONObject o2j(Object obj) {
        return J.parseObject(o2s(obj));
    }

    public static List<JSONObject> o2l(Object obj) {
        return J.parseArray(o2s(obj), JSONObject.class);
    }
    public static List<JSONObject> s2l(String obj) {
        return J.parseArray(obj, JSONObject.class);
    }
    public static JSONObject s2j(String obj) {
        return J.parseObject(obj);
    }

    public static <T> T o2m(Object object, Class<T> cls){
        return J.parseObject(o2s(object), cls);
    }

    public static HashMap o2m(Object object){
        return J.parseObject(o2s(object), HashMap.class);
    }
    public static HashMap o2m(String object){
        return J.parseObject(object, HashMap.class);
    }
    public static HashMap s2m(String object){
        return J.parseObject(object, HashMap.class);
    }
    public static <T> T s2m(String object, Class<T> cls){
        return J.parseObject(object, cls);
    }

    public J() {
    }
    public static J create() {
        return new J();
    }

    public static J create(String key, Object val) {
        return create().put(key, val);
    }
    public static List<JSONObject> list(){
        List<JSONObject> list = new ArrayList<>();
        return list;
    }

    public static List<JSONObject> list(JSONObject... objects){
        var list = list();
        for(var obj:objects){
            list.add(obj);
        }
        return list;
    }

    public J put(String key, Object val) {
        super.put(key, val);
        return this;
    }

    public static String s2r(String message) {
        JSONObject object = new JSONObject();
        object.put("message", message);
        object.put("result", new JSONObject());
        object.put("isRefresh", 0);
        return o2s(object);
    }
}
